/**
 * Created by AllanXu on 01/03/2016.
 */
define(function (require) {
	var Vue = require('vue'),
		Config = require('config'),
		_ = require('ajax'),
		utilities = require('utilities');

	var type = utilities.getParameterByName('type'), page = utilities.getParameterByName('page');
	var url = page == 1 ? Config.SERVICE.USERCENTER.USER_LIST : Config.SERVICE.USERCENTER.GREET_LIST;

	var app = new Vue({
		el: '#uc_userlist',
		data: {
			renderObj: {},
			type: type,
			page: page,
			req: {
				id: sessionStorage.id,
				type: type,
				pageno: 1,
				pagesize: 50
			},
			picServer: Config.picServer,
			titles: [],
			titles1: ['我的关注', '我的粉丝', '我看过谁', '谁看过我'],
			titles2: ['我的留言', '给我留言', '我的招呼', '招呼我的'],
            // 打招呼
            msgPageno: 1,
            msgDialog: false,
            msgPageCount: '',
            msgList: [],
            selectMsgIndex: -1,
            selectMsg: '',
            mateId: '',
			showReply: false,
			showComplain: false,
			publicContent: '',
			// 回留言、投诉、拉黑对象用户id
			targetUserId: 0
		},
		methods: {
			init: function () {
				app.titles = page == 1 ? app.titles1 : app.titles2;
				_(url, app.req, function (data) {
					app.renderObj = data.pageitems;
				});
			},
			go: function (url) {
				location.href = url;
			},
			// 回留言
			reply: function (id) {
				app.targetUserId = id;
				app.showReply = true;
			},
			// 投诉
			complain: function (id) {
				app.targetUserId = id;
				app.showComplain = true;
			},
			// 拉黑
			blackList: function (id) {
				confirm('确认拉黑该用户？');
				if (confirm) {
					_(Config.SERVICE.USERCENTER.BLACK_SAVE, {
						'blackList.basicA.id': sessionStorage.id,
						'blackList.basicB.id': id
					}, function (data) {
						if (data.type === 'SUCCESS') {
							app.init();
						}
					});
				}
			},
            switchSayhi: function () {
                app.msgDialog = !app.msgDialog;
            },
            sayhi: function (mateId) {
                // 是否认证/禁用
                var userInfo = JSON.parse(decodeURIComponent(atob(sessionStorage.userInfo)));
                if (userInfo.member.sfrz == 0) {    // 认证
                    authDialog.show();
                } else if (userInfo.member.sfjy == 1) {  // 禁用
                    forbidden.show();
                } else {
                    console.log("已认证、没有禁用");

                    _(Config.SERVICE.MATES.VERIFY+'?id=' + sessionStorage.id, {}, function(data) {
                        console.log(data,2444444);
                        if(data.errmsg.indexOf("择偶") > 0 ){
                            // 择偶条件没有完善跳转到择偶条件
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中完善择偶条件。")){
                                location.href = 'uc_mate_condition.html';
                            }
                        }else if(data.errmsg.indexOf("基本信息") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中完善个人资料。")){
                                location.href = 'uc_profile.html';
                            }
                        }else if(data.errmsg.indexOf("形象照") > 0){
                            if(confirm("为更好地为您提供服务，请在“个人编辑”中上传形象照。")){
                                location.href = 'uc_basicInfo.html';
                            }
                        }else{
                            app.msgDialog = true;
                            app.mateId = mateId;
                            _(Config.SERVICE.MATES.GREETEDLIST+'?pageno='+app.msgPageno+'&pagesize=5', {}, function(data) {
                                app.msgPageCount = data.pagecount;
                                app.msgList = data.pageitems;
                            });
                        }
                    });
                }
            },
            exchangeMsg: function () {
                if (app.msgPageno >= app.msgPageCount) {
                    app.msgPageno = 1;
                } else {
                    app.msgPageno++;
                }
                app.sayhi(app.mateId);
            },
            selectMsgFn: function (index, content) {
                console.log(index)
                app.selectMsgIndex = index;
                app.selectMsg = content;
            },
            submitMsg: function () {
                if (app.mateId == sessionStorage.id) {
                    alert("不能发送给自己~");
                } else {
                    _(Config.SERVICE.MATES.SAYHI + '?conMsg.memberA.id=' + sessionStorage.id + '&conMsg.memberB.id=' + app.mateId + '&conMsg.msg=' + app.selectMsg + '&conMsg.type=1', {}, function (data) {
                        console.log(data);
                        if (data.code == 200) {
                            app.selectMsgIndex = -1;
                            app.selectMsg = '';
                            app.msgDialog = false;
                            alert("发送成功");
                        }
                    });
                }
            },
			hideDialog: function () {
				app.showReply = false;
				app.showComplain = false;
			},
			submitContent: function () {
				if (app.publicContent.trim() === '') {
					alert('内容不能为空！');
				} else {
					if (app.showReply) {
						// 回复
						_(Config.SERVICE.USERCENTER.REPLY, {
							'conMsg.memberA.id': sessionStorage.id,
							'conMsg.memberB.id': app.targetUserId,
							'conMsg.msg': app.publicContent,
							'conMsg.type': app.type == 4 ? 1 : 2
						}, function (data) {
							console.log(data);
                            alert("发送成功");
							app.hideDialog();
						});
					} else {
						// 投诉
						_(Config.SERVICE.USERCENTER.COMPLAIN, {
							'complaint.basicA.id': sessionStorage.id,
							'complaint.basicB.id': app.targetUserId,
							'complaint.content': app.publicContent
						}, function (data) {
							console.log(data);
							app.hideDialog();
						});
					}
				}
			}
		}
	});
	app.init();
});
